package leeon.bbs;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;

import leeon.util.HtmlUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UserHelper {
	
	private static Log logger = LogFactory.getLog(UserHelper.class);

	public static final String BASE_DIR = "D:\\project\\eurospace\\forpeddy\\bisc\\";
	private File dir = new File(BASE_DIR);
	
	public UserHelper() {
	}
	
	public void parse() throws IOException {
		String[] files = dir.list(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				if (name.startsWith("user") && name.endsWith(".txt"))
					return true;
				else
					return false;
			}
		});
		logger.info("set define off;");
		for (String f : files) {
			parseOneFile(new File(BASE_DIR + f));
			logger.info("commit;");
		}
		logger.info("set define on;");
		
	}
	
	public void parseOneFile(File file) throws IOException {
		if (file.exists()) {
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = null;
			StringBuffer sb = new StringBuffer();
			while ((line = reader.readLine()) != null) {
				if (line.trim().equals("")) {
					parseOneUser(sb.toString(), file.getName().substring(4, 18));
					sb.setLength(0);
				} else {
					sb.append(line);
				}
			}
			reader.close();
		}
	}
	
	public void parseOneUser(String userData, String date) {
		if (userData == null || userData.trim().length() == 0) return; 
		
		
		String id = HtmlUtil.findTag(userData, "", " ");
		
		id = ("没有个人说明档上".equals(id))?"guest":id;
		String nick = HtmlUtil.findTag(userData, " (", ")");
		nick = (nick==null)?"访客":nick;
		nick = (nick.length()>50)?nick.substring(0, nick.indexOf('?')):nick;
		String star = HtmlUtil.findTag(userData, " 次  [", "]");
		star = (star==null)?"未知":star;
		
		String time = HtmlUtil.findTag(userData, "上 次 在:[", "]");
		time = time.replaceAll("年|月", "-").replace('日', ' ').replace('天', '日');
		String ip = HtmlUtil.findTag(userData, "从 [", "]");
		
		String login = HtmlUtil.findTag(userData, "共上站 ", " 次");
		login = ("guest".equals(id))?"0":login;
		
		String perf = HtmlUtil.findTag(userData, "表现值:[", "]");
		String life = HtmlUtil.findTag(userData, "生命力:[", "]");
		String article = HtmlUtil.findTag(userData, "文 章 数:[", "]");
		String exp = HtmlUtil.findTag(userData, "经 验 值:[", "]");
		
		String status = HtmlUtil.findTag(userData, "身份: ");
		
//		System.out.println("ID:[" + id + "], 昵称:[" + nick + "], 星座:[" + star + "], 扫描日期:[" + date +"]");
//		System.out.println("上次登录时间:[" + time + "], 地址:[" + ip + "], 身份:" + status);
//		System.out.println("表现值:[" + perf + "], 生命值:[" + life + "], 经验值:[" + exp + "], 文章数:[" + article + "]");
		
		StringBuffer sql = new StringBuffer();
		sql.append("insert into bbsuser_temp (")
		.append("id, nick, star, ")
		.append("last_login_date, last_login_ip, ")
		.append("performance, life, experience, article, login, ")
		.append("status, scan_date) values ('")
		.append(id).append("', '").append(nick.replace("'", "''")).append("', '").append(star).append("', ")
		.append("to_date('").append(time).append("','yyyy-mm-dd hh24:mi:ss day'), '").append(ip).append("', '")
		.append(perf).append("', '").append(life).append("', '").append(exp).append("', '").append(article).append("', '").append(login).append("', '")
		.append(status).append("', to_date('").append(date).append("','yyyymmddhh24miss'));");
		logger.info(sql.toString());
	}
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		UserHelper user = new UserHelper();
		user.parse();
	}

}
